package com.ruoyi.web.controller.system;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ruoyi.common.annotation.HasPermi;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.dao.SysRole;
import com.ruoyi.system.domain.dao.SysUser;
import com.ruoyi.system.domain.dao.SysUserRole;
import com.ruoyi.system.domain.persist.SysRolePo;
import com.ruoyi.system.domain.query.SysRoleQo;
import com.ruoyi.system.domain.query.SysUserQo;
import com.ruoyi.system.domain.view.SysRoleVo;
import com.ruoyi.system.domain.view.SysUserVo;
import com.ruoyi.system.domain.wrapper.query.SysRoleWrapperQ;
import com.ruoyi.system.domain.wrapper.query.SysUserWrapperQ;
import com.ruoyi.system.domain.wrapper.view.SysRoleWrapperV;
import com.ruoyi.system.domain.wrapper.view.SysUserWrapperV;
import com.ruoyi.system.service.ISysRoleService;
import com.ruoyi.system.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * 角色信息
 * 
 * @author ruoyi
 */
/**
 * 角色信息Controller
 *
 * @author 尹国春
 * @date 2022-07-14
 */
@Api(value = "角色信息", tags = {"角色信息"})
@RestController
@RequestMapping("/system/role")
public class SysRoleController extends BaseController
{
    @Autowired
    private ISysRoleService sysRoleService;

    @Autowired
    private ISysUserService userService;

    /**
     * 角色信息分页查询
     */
    @ApiOperation(value="角色信息分页查询",notes="分页参数：pageNum,pageSize,orderByColumn(数据库字段名称),isAsc(asc|desc)")
    @HasPermi("system:role:list")
    @GetMapping("/list")
    public TableDataInfo<SysRoleVo> list(SysRoleQo sysRole)
    {
        IPage<SysRole> page=sysRoleService.page(sysRole.getPage(), SysRoleWrapperQ.getQueryWrapper(sysRole));
        return SysRoleWrapperV.build().pageVO(page);
    }
    /**
     * 角色信息导出
     */
    @ApiOperation("角色信息导出")
    @HasPermi("system:role:list")
    @Log(title = "角色信息导出", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysRoleQo sysRole)
    {
        List<SysRole> list = sysRoleService.list(SysRoleWrapperQ.getQueryWrapper(sysRole));
        List<SysRoleVo> voList = SysRoleWrapperV.build().listVO(list);
        ExcelUtil<SysRoleVo> util = new ExcelUtil<>(SysRoleVo.class);
        util.exportExcel(response, voList, "角色信息数据");
    }

    /**
     * 获取角色信息详细信息
     */
    @ApiOperation("角色信息信息")
    @HasPermi("system:role:list")
    @GetMapping(value = "/get/{roleId}")
    public AjaxResult getInfo(@PathVariable("roleId") String roleId)
    {
        SysRole entity=sysRoleService.getById(roleId);
        return AjaxResult.success(SysRoleWrapperV.build().entityVO(entity));
    }

    /**
     * 新增角色信息
     */
    @ApiOperation("角色信息新增")
    @HasPermi("system:role:edit")
    @Log(title = "新增角色信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody SysRolePo sysRole)
    {
        if (UserConstants.NOT_UNIQUE.equals(sysRoleService.checkRoleNameUnique(sysRole)))
        {
            return AjaxResult.error("新增角色'" + sysRole.getRoleName() + "'失败，角色名称已存在");
        }
        else if (UserConstants.NOT_UNIQUE.equals(sysRoleService.checkRoleKeyUnique(sysRole)))
        {
            return AjaxResult.error("新增角色'" + sysRole.getRoleName() + "'失败，角色权限已存在");
        }
        return toAjax(sysRoleService.insertRole(sysRole));
    }

    /**
     * 修改角色信息
     */
    @ApiOperation("角色信息修改")
    @HasPermi("system:role:edit")
    @Log(title = "修改角色信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody SysRolePo sysRole)
    {
        if (UserConstants.NOT_UNIQUE.equals(sysRoleService.checkRoleNameUnique(sysRole)))
        {
            return AjaxResult.error("修改角色'" + sysRole.getRoleName() + "'失败，角色名称已存在");
        }
        else if (UserConstants.NOT_UNIQUE.equals(sysRoleService.checkRoleKeyUnique(sysRole)))
        {
            return AjaxResult.error("修改角色'" + sysRole.getRoleName() + "'失败，角色权限已存在");
        }
        return toAjax(sysRoleService.updateRole(sysRole));
    }

    /**
     * 状态修改
     */
    @HasPermi("system:role:edit")
    @Log(title = "角色管理", businessType = BusinessType.UPDATE)
    @PostMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody SysRole role)
    {
        role.setUpdateBy(getUsername());
        return toAjax(sysRoleService.updateRoleStatus(role));
    }

    /**
     * 删除角色信息
     */
    @ApiOperation("角色信息删除")
    @HasPermi("system:role:remove")
    @Log(title = "删除角色信息", businessType = BusinessType.DELETE)
    @PostMapping("/delete/{roleIds}")
    public AjaxResult remove(@PathVariable String[] roleIds)
    {
        return toAjax(sysRoleService.removeByIds(Arrays.asList(roleIds)));
    }

    /**
     * 获取角色选择框列表
     */
    @HasPermi("system:role:list")
    @GetMapping("/optionselect")
    public AjaxResult optionselect()
    {
        return AjaxResult.success(sysRoleService.list(null));
    }

    /**
     * 查询已分配用户角色列表
     */
    @ApiOperation(value="查询已分配用户角色列表",notes="分页参数：pageNum,pageSize,orderByColumn(数据库字段名称),isAsc(asc|desc)")
    @HasPermi("system:role:list")
    @GetMapping("/authUser/allocatedList")
    public TableDataInfo<SysUserVo> allocatedList(SysUserQo sysUser)
    {
        IPage<SysUser> page=userService.page(sysUser.getPage(), SysUserWrapperQ.getQueryWrapper(sysUser));
        return SysUserWrapperV.build().pageVO(page);
    }

    /**
     * 查询未分配用户角色列表
     */
    @ApiOperation(value="查询未分配用户角色列表",notes="分页参数：pageNum,pageSize,orderByColumn(数据库字段名称),isAsc(asc|desc)")
    @HasPermi("system:role:list")
    @GetMapping("/authUser/unallocatedList")
    public TableDataInfo<SysUserVo> unallocatedList(SysUserQo sysUser)
    {
        IPage<SysUser> page=userService.page(sysUser.getPage(), SysUserWrapperQ.getQueryWrapper(sysUser));
        return SysUserWrapperV.build().pageVO(page);
    }

    /**
     * 取消授权用户
     */
    @HasPermi("system:role:edit")
    @Log(title = "角色管理", businessType = BusinessType.GRANT)
    @PutMapping("/authUser/cancel")
    public AjaxResult cancelAuthUser(@RequestBody SysUserRole userRole)
    {
        return toAjax(sysRoleService.deleteAuthUser(userRole));
    }

    /**
     * 批量取消授权用户
     */
    @HasPermi("system:role:edit")
    @Log(title = "角色管理", businessType = BusinessType.GRANT)
    @PutMapping("/authUser/cancelAll")
    public AjaxResult cancelAuthUserAll(String roleId, String[] userIds)
    {
        return toAjax(sysRoleService.deleteAuthUsers(roleId, userIds));
    }

    /**
     * 批量选择用户授权
     */
    @HasPermi("system:role:edit")
    @Log(title = "角色管理", businessType = BusinessType.GRANT)
    @PutMapping("/authUser/selectAll")
    public AjaxResult selectAuthUserAll(String roleId, String[] userIds)
    {
        return toAjax(sysRoleService.insertAuthUsers(roleId, userIds));
    }
}
